<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>编辑产品需求详情</title>
    <link href="../css/list-detial.css" type="text/css" rel="stylesheet"/>
</head>
<body>
<!--header-->
<header id="header" class="header">
    <div class="header-info left"><i></i><a onclick="toNedDetail()" href="javascript:">产品需求详情</a>&nbsp;>&nbsp;编辑产品需求</div>
    <div class="right-user right">
         <div class="clear t" >             <div id="headusername" class="role"></div>             <div class="icon"></div>         </div>
        <div class="head-down">
            <ul><li><a href="edit-pwd.html">修改密码</a></li><li><a onclick="loginOut()">退出系统</a></li></ul></div><div>
    </div>
    </div>
</header>
<!--menu-->
<aside id="aside" class="aside">
    <div class="logo">运营平台</div>
    <ul class="menu" id="asidemenu">
    </ul>
</aside>

<!--content-->
<div class="content">
    <div class="page-inner">
        <div class="page-content">
            <div class="table-head clear"><div class="left document-name" id="document-name"><i class="bug-i"></i></div><div class="right edit"><a  onclick="returnList()" class="sure">取消</a><a onclick="commitEdit()" class="sure">保存</a></div></div>
           
            <div class="table-cont">
                <table class="content-table">
                    <tr>
                        <td><span class="red-start">*</span>需求名称：</td>
                        <td colspan="3" ><input type="text" id="name" style="width: 91%"/></td>
                    </tr>
                    <tr>
                        <td><span class="red-start">*</span>类别：</td>
                        <td><select id="typeList"></select></td>
                        <td><span class="red-start">*</span>产品：</td>
                        <td><select id="productList" onchange="_API.getFunctionList('functionList',false,false,'productList')"></select></td>
                    </tr>
                    <tr>
                        <td><span class="red-start">*</span>使用平台：</td>
                        <td><select id="platformList"></select></td>
                        <td><span class="red-start">*</span>功能模块：</td>
                        <td><select id="functionList"><option>模块三</option></select></td>
                    </tr>
                    <tr>
                        <td>优先级别：</td>
                        <td><select id="priority"></select></td>
                        <td>计划版本：</td>
                        <td><input type="text" id="version"/></td>
                        </tr>
                    <tr>
                        <td>处理状态：</td>
                        <td><select id="status"></select></td>
                        <td>预计工时：</td>
                        <td><input type="text" id="working-hours"/> 小时</td>
                    </tr>
                    <tr class="area">
                        <td>简述：</td>
                        <td colspan="3">
                            <textarea id="discribe"></textarea>
                        </td>
                    </tr>
                    <tr class="area">
                        <td>方案思路：</td>
                        <td colspan="3">
                            <textarea id="solution"></textarea>
                        </td>
                    </tr>
                    <tr class="area">
                        <td>附件：</td>
                        <td colspan="3">
                            <a href="javascript:;" class="a-upload" id="fileUpload"><input id="file" value="请选择" type="file"/>点击这里上传</a>
                            <p class="format">支持的格式有：JPG，PNG，PDF，PPT，XLSX，XLS，CSV，DOCX，DOC，ZIP</p>
                            <p class="format">文件名不能包含*、#等特殊符号</p>
                            <ul class="addAttachmentList" id="addAttachmentList">
                            </ul>
                        </td>
                    </tr>
                </table>
            </div>

            <div class="table-head clear"><div class="left document-name"><i class="bug-i"></i>相关原始需求</div></div>
            <div class="table-cont">
                <table class="relative-list">
                    <thead>
                        <tr>
                            <th>来源</th>
                            <th>需求方</th>
                            <th>需求信息</th>
                            <th>紧急程度</th>
                            <th>处理状态</th>
                           
                        </tr>
                    <thead>
                     <tbody id="relativeList">
                       
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
<script src="../js/jquery.min.js"></script>
<script src="../js/jquery.cookie.js"></script>
<script src="../js/common.js"></script>

<script type="text/javascript">
    var _id = getUrlId();
    function returnList(){
        location.href = 'pm-need-detail-pm.html?id='+_id;
    }
    function toNedDetail() {
        location.href = 'pm-need-detail-pm.html?id='+getUrlId();
    }

    // 上传文件
    var attachment = new attachment();
    attachment.init({
        fileUpload:$('#fileUpload'),
        addAttachmentList:$('#addAttachmentList')
    }).deleteFile();
    //获取
    updata();
    function updata(){
        var url  = _ip+'/product/getRequireById?callback=update&id='+_id;
        $.ajax({
            url:url,
            type: 'GET',
            dataType: 'jsonp',
            contentType:'application/json',
            jsonpCallback: 'update',
            success:function(data){
                if(data.success){
                    var data = data.data;
                    $('#document-name').append('ID.'+data.id)
                    _API.getTypeList('typeList',data.type)
                        .getProductList('productList',data.product)
                        .getPlatformList('platformList',data.plat)
                        .getFunctionList('functionList',data.feature, true, 'productList', data.product)
                        .getPriorityList('priority',data.priority)
                        .getStatusListPm('status',data.status);
                    $('#name').val(data.name);
                    $('#version').val(data.version);
                    $('#discribe').val(data.discribe);
                    $('#solution').val(data.solution);
                    $('#working-hours').val(data.expectWorkhour);
                    data.attachment && attachment.getAttachmentList(data.attachment.split(','));
                }else{
                    alert('更新失败, 请稍后重试！');
                }
            }
        })
    }


    var needName = $('#name');
    functionList = $('#functionList');
    needName.focus(function(){
        needName.removeClass('error-i');
    })

    // 提交编辑
    function commitEdit() {
        if(needName.val() == ''){
            needName.addClass('error-i');
            return
        }
        if($('#functionList').find('option:selected').text() == '请选择'){
            alert('请选择功能模块！');
            return false
        }
        var attachment = [];
        var li = $('#addAttachmentList').find('li');
        if(li.length >= 1){
            li.each(function(){
                attachment.push( $(this).attr('rel') );
            })
        }

        var url  = _ip+'/product/updateRequire';
        var data = {
            "id":_id,
            "type": $('#typeList').val(),
            "product": $('#productList').val(),
            "plat": $('#platformList').val(),
            "feature":$('#functionList').val(),
            "name":$('#name').val(),
            "priority":$('#priority').val(),
            "version":$('#version').val() || null,
            "status":$('#status').val(),
            "attachment":attachment.join(','),
            "discribe":$('#discribe').val() || null,
            "solution":$('#solution').val() || null,
            "expectWorkhour": $('#working-hours').val() || null
        }
        var hourFormat = /^[0-9]+(.[0-9])?$/;
        var nameValidate = hourFormat.test(data.expectWorkhour);
        if (data.expectWorkhour) {
            if(!nameValidate) {
                alert('计划工时必须是正整数或一位小数');
                return false;
            }
            else {
                data.expectWorkhour = parseFloat(data.expectWorkhour);
            }
        }
        else if(data.expectWorkhour === 0){
            alert('计划工时必须是正整数或一位小数');
            return false;
        }
        $.ajax({
            url:url,
            type : "POST",
            xhrFields: {
                withCredentials: true
            },
            crossDomain: true,
            dataType: 'json',
            contentType:'application/json',
            data:JSON.stringify(data),
            success:function(data){
                if(data.message === 'LOGIN') {
                    window.location.href = 'user-login.html';
                    return false;
                }
                if(data.success){
                    location.href = 'pm-need-detail-pm.html?id='+_id;
                }else{
                    alert(data.message);
                }
            }
        })
    }
    getRelativList();
    function getRelativList(){
        var url  = _ip+'/requireRelation/getRequireRelationByPid?pid='+_id;        
        $.ajax({
            url:url,
            type: 'GET',
            dataType: 'jsonp',
            contentType:'application/json',
            jsonpCallback: 'off',
            success:function(data){
                var data = data.data;
                var h = '';

                for(var k = 0; k < data.length; k++){
                    h += '<tr><td>'+data[k].pName+'</td>';
                        h += '<td>'+data[k].customerName+'</td>';
                        h += '<td><a href="pm-need-detail-mems.html?id='+data[k].id+'">'+( data[k].productName + '&nbsp;|&nbsp;' + data[k].feature+ '&nbsp;|&nbsp;' +(data[k].name || '')    )+'</a></td>';
                        h += '<td>'+data[k].priority+'</td>';
                        h += '<td>'+data[k].status+'</td>';
                }
                $('#relativeList').html(h);
            }
        })
    }

</script>
</body>
</html>